﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_Tintuc_TypeCategoryNews_Info : System.Web.UI.UserControl
{
    TblDanhMucTinManager _TblDanhMucTinManager = new TblDanhMucTinManager();
    public int CateID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { CateID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { CateID = 0; }

            loadCateParent();

            LoadViTri(ddlCatePoi);

            if (CateID == 0) //Add cate
            {
                lblTitle.Text = "Thêm loại danh mục";
                Toolbox1.ShowBack = Toolbox1.ShowRefesh = Toolbox1.ShowDel = false;
            }
            else //sua cate
            {
                imgbtn_Delete.Enabled = true;
                lblDelete.Visible = true;
                imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16.gif";
                lblTitle.Text = "Sửa loại danh mục: " + CateID;

                BindData(CateID);
            }
        }
    }

    protected void BindData(int ID)
    {
        TblDanhMucTinEntity _TblDanhMucTinEntity = _TblDanhMucTinManager.SelectOne(ID);
        if (_TblDanhMucTinEntity == null) { lblthongbao.Text = "<img src='../../Images/validatebox_warning.png' alt='Cảnh báo' width='16' height='16' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        hidID.Value = ID.ToString();
        txtID.Text = ID.ToString();
        //fckMoTa.Value = _TblDanhMucTinEntity.MoTa;
        txtNameCate.Text = _TblDanhMucTinEntity.TenDanhMuc;
        txtOrder.Text = _TblDanhMucTinEntity.ThuTu.ToString();
        string dmcha = _TblDanhMucTinEntity.DanhMucCha.ToString();
        ddlCateParent.SelectedValue = dmcha;
        ddlCatePoi.SelectedValue = _TblDanhMucTinEntity.ViTri;
        img_news.Visible = true;
        img_news.ImageUrl = (_TblDanhMucTinEntity.Icon != "") ? _TblDanhMucTinEntity.Icon : "~/AdminPortal/Images/icon/no_image.jpg";
        if (string.IsNullOrEmpty(_TblDanhMucTinEntity.Icon))
        {
            imgbtn_Delete.Enabled = false;
            imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16_hiden.gif";
            img_news.Visible = false;
        }
        else
        {
            img_news.Visible = true;
        }
        chkIsHome.Checked = Convert.ToBoolean(_TblDanhMucTinEntity.IsHome);
        chkStatus.Checked = Convert.ToBoolean(_TblDanhMucTinEntity.TrangThai);
    }

    private void loadCateParent()
    {
        TblDanhMucTinEntity _TblDanhMucTinEntity = new TblDanhMucTinEntity();
        _TblDanhMucTinEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblDanhMucTinEntity.Type = "Typecategory";
        DataTable _dtDM = _TblDanhMucTinManager.be_SelectToDropdownlist(_TblDanhMucTinEntity);
        if (_dtDM != null || _dtDM.Rows.Count > 0)
            HNG.LIB.BIND_SOURCE_V3.Bind_DropDownList(ddlCateParent, _dtDM, TblDanhMucTinFields.TenDanhMuc.Name, TblDanhMucTinFields.Id.Name, TblDanhMucTinFields.DanhMucCha.Name, true);
    }

    public void LoadViTri(DropDownList _ddl)
    {
        _ddl.Items.Clear();
        try
        {
            _ddl.Items.Add(new ListItem("Trên", enumViTri.top.ToString()));
            _ddl.Items.Add(new ListItem("Dưới", enumViTri.bottom.ToString()));
            _ddl.Items.Add(new ListItem("Trái", enumViTri.left.ToString()));
            _ddl.Items.Add(new ListItem("Phải", enumViTri.right.ToString()));

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblDanhMucTinEntity _TblDanhMucTinEntity = _TblDanhMucTinManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblDanhMucTinEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.Icon))
                {
                    string ImageDefault = Server.MapPath(_TblDanhMucTinEntity.Icon);
                    if (File.Exists(ImageDefault)) File.Delete(ImageDefault);
                }
            }
            _TblDanhMucTinManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/CategoryNewsEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        if (txtNameCate.Text == "") { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Nhập tên danh mục !</span>"; txtNameCate.Focus(); return; }
        TblDanhMucTinEntity _TblDanhMucTinEntity = new TblDanhMucTinEntity();
        if (!string.IsNullOrEmpty(hidID.Value)) _TblDanhMucTinEntity.Id = Convert.ToInt32(hidID.Value);//update
        _TblDanhMucTinEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblDanhMucTinEntity.TenDanhMuc = txtNameCate.Text;
        if (ddlCateParent.SelectedValue != hidID.Value)
            _TblDanhMucTinEntity.DanhMucCha = Convert.ToInt32(ddlCateParent.SelectedValue);
        _TblDanhMucTinEntity.TenDanhMucCha = ddlCateParent.SelectedItem.Text;
        _TblDanhMucTinEntity.ViTri = ddlCatePoi.SelectedValue;
        _TblDanhMucTinEntity.ThuTu = HNG.LIB.Common.CheckErr.CheckInputValidate(txtOrder.Text, 1);
        _TblDanhMucTinEntity.TrangThai = (chkStatus.Checked == true) ? true : false;
        _TblDanhMucTinEntity.IsHome = (chkIsHome.Checked == true) ? true : false;
        //_TblDanhMucTinEntity.MoTa = fckMoTa.Value;
        _TblDanhMucTinEntity.Type = "Typecategory";
        if (FileUpload1.FilePath != "")
        {
            string ImageDefault = Server.MapPath(_TblDanhMucTinEntity.Icon);
            if (File.Exists(ImageDefault)) File.Delete(ImageDefault);
            _TblDanhMucTinEntity.Icon = FileUpload1.FilePath;
        }

        if (!string.IsNullOrEmpty(hidID.Value))
            _TblDanhMucTinManager.Update(_TblDanhMucTinEntity);//update
        else
        {
            _TblDanhMucTinManager.Insert(_TblDanhMucTinEntity);//Insert
            hidID.Value = _TblDanhMucTinEntity.Id.ToString();
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void imgbtn_Delete_Click(object sender, ImageClickEventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblDanhMucTinEntity _TblDanhMucTinEntity = _TblDanhMucTinManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblDanhMucTinEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblDanhMucTinEntity.Icon))
                {
                    string defaultImage = Server.MapPath(_TblDanhMucTinEntity.Icon);
                    if (File.Exists(defaultImage)) File.Delete(defaultImage);
                    _TblDanhMucTinEntity.Icon = "";
                    _TblDanhMucTinManager.Update(_TblDanhMucTinEntity);
                }
            }
        }
        BindData(CateID);
    }
}